import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router"

const routes: Array<RouteRecordRaw> = [
    {
        path: '',
        redirect: () => {
            return {path: '/home'}
        },
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('/src/pages/login/Login.vue'),
    },
    {
        path: '/tabar',
        name: 'tabar',
        component: () => import('/src/pages/tabars/Index.vue'),
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('@/pages/tabars/home/Home.vue'),
                meta: {
                    title: ''
                }
            },
            {
                path: '/classify',
                name: 'classify',
                component: () => import('@/pages/tabars/classify/Classify.vue'),
                meta: {title: '商品分类'}
            },
            {
                path: '/my',
                name: 'my',
                component: () => import('@/pages/tabars/my/My.vue'),
                meta: {title: '我的'},
            }
        ]
    },
    {
        path: '/shopCart',
        name: 'shopCart',
        component: () => import('@/pages/tabars/shop-cart/ShopCart.vue'),
        meta: {
            title: '购物车'
        }
    },
    {
        path: '/addressList',
        name: 'addressList',
        component: () => import('@/pages/address/AddressList.vue'),
        meta: {
            title: '配送地址'
        }
    },
    {
        path: '/addAddress',
        name: 'addAddress',
        component: () => import('@/pages/address/AddAddress.vue'),
        meta: {
            title: '编辑配送地址'
        }
    },
    {
        path: '/:currentPath(.*)*', // 路由未匹配到，进入这个
        redirect: () => {
            return {path: '/404'}
        },
    },
];
const router = createRouter({
    history: createWebHistory(),
    routes,
});
router.beforeEach((to: any, from, next) => {
    document.title = to?.meta?.title || '爱豆荣森';
    // 移除应用所有上传文件input
    document.querySelectorAll('.appInputFile').forEach(item => (item.remove()));
    next();
});

export default router